#!/usr/bin/env python
# -*- coding:utf-8 -*-

# file:do_exam1.py
# author:王雯雯
# datetime:2024-12-7
# software: PyCharm
import socket

# 创建套接字
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定本地地址和端口
server_address = ('localhost', 8888)
server_socket.bind(server_address)

# 监听端口，允许最多5个连接同时等待
server_socket.listen(5)
print('服务器正在监听端口 8888...')

while True:
    # 等待客户端连接
    client_socket, client_address = server_socket.accept()
    print(f'与客户端 {client_address} 建立连接')

    try:
        while True:
            # 接收数据，每次最多接收1024字节
            data = client_socket.recv(1024)
            if data:
                # 将接收到的数据解码后打印输出
                print(f'接收到的数据: {data.decode("utf-8")}')
            else:
                break
    finally:
        # 关闭与客户端的连接
        client_socket.close()
